﻿using System;
using System.Data.Common;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections.ObjectModel;
using MySql.Data.MySqlClient;
using Reviewer.Utilities.Data.MySql;
using System.Globalization;
using System.Data.SqlClient;

namespace Reviewer.Data.Bases
{
    /// <summary>
    /// Repository base class
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class RepositoryBase<T> where T : class
    {
        #region methods (protected)
        protected T ExecuteReaderAndBuildItem(DbCommand command)
        {
            using (IDataReader reader = ExecuteReader(command))
            {
                if (reader.Read())
                {
                    T item = BuildItemFromReader(reader);
                    return item;
                }
            }
            return null;
        }

        protected Collection<T> ExecuteReaderAndBuildList(DbCommand command)
        {
            Collection<T> list = new Collection<T>();
            using (IDataReader reader = ExecuteReader(command))
            {
                while (reader.Read())
                {
                    T item = BuildItemFromReader(reader);
                    list.Add(item);
                }
            }
            return list;
        }

        protected IDataReader ExecuteReader(DbCommand command)
        {
            MySqlConnection connection = new MySqlConnection();
            connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Reviewer.Data"].ConnectionString;
            connection.Open();
            command.Connection = connection;
            return command.ExecuteReader(CommandBehavior.CloseConnection);
        }

        protected int ExecuteNonQuery(DbCommand command)
        {
            using (MySqlConnection connection = new MySqlConnection())
            {
                connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Reviewer.Data"].ConnectionString;
                connection.Open();
                try
                {
                    command.Connection = connection;
                    return command.ExecuteNonQuery();
                }
                finally
                {
                    connection.Close();
                }
            }
        }

        protected object ExecuteScalar(string commandText)
        {
            using (MySqlCommand command = new MySqlCommand(commandText))
            {
                return this.ExecuteScalar(command);
            }
        }

        protected object ExecuteScalar(DbCommand command)
        {
            using (MySqlConnection connection = new MySqlConnection())
            {
                connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Reviewer.Data"].ConnectionString;
                connection.Open();
                try
                {
                    command.Connection = connection;
                    return command.ExecuteScalar();
                }
                finally
                {
                    connection.Close();
                }
            }
        }

        protected DataSet FillDataSet(IDbCommand command)
        {
            using (MySqlConnection connection = new MySqlConnection())
            {
                connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Reviewer.Data"].ConnectionString;
                connection.Open();
                try
                {
                    command.Connection = connection;
                    using (MySqlDataAdapter adapter = new MySqlDataAdapter((MySqlCommand)command))
                    {
                        DataSet result = new DataSet();
                        result.Locale = CultureInfo.CurrentCulture;
                        adapter.Fill(result);
                        return result;
                    }
                }
                finally
                {
                    connection.Close();
                }
            }
        }

        protected abstract T BuildItemFromReader(IDataReader reader);
        #endregion
    }
}
